<template>
  <div class="app-container">
    <div class="table_content">
      <div class="btn-btm">
        <el-button type="primary" plain size="mini" icon="el-icon-plus" @click="handleAdd">上传检查记录</el-button>
      </div>
      <!--表格渲染-->
      <el-table ref="table" v-loading="loading" :data="list" style="width: 100%;" border
                :header-cell-style="{background: '#F8F8F9',padding: '6px 0 !important',color: '#515a6e'}">
        <af-table-column prop="projectName" fixed="left" label="项目名称" />
        <af-table-column prop="recordDate" label="检查日期" />
        <af-table-column prop="isRectify" label="是否需要整改" >
          <template slot-scope="scope">
            {{{'0':'不需要','1':'需要'}[scope.row.isRectify]}}
          </template>
        </af-table-column>
        <af-table-column prop="userName" label="监督用户名" />
        <af-table-column prop="addTime" label="添加时间" />
      </el-table>

      <!--分页-->
      <pagination v-show="total>0" :total="total"
                  :page.sync="listQuery.pageNum"
                  :limit.sync="listQuery.pageSize"
                  @pagination="getList" />
    </div>

    <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible" :close-on-click-modal="false">
      <el-form :model="editForm" label-width="120px" :rules="editFormRules" ref="editForm">
        <el-row>
          <el-col :span="12">
            <el-form-item label="项目名称:" prop="projectID" class="form-item">
              <el-select placeholder="请选择" style="width:100%;" clearable v-model="editForm.projectID"
                         class="form-select" :popper-append-to-body="false" popper-class="select-popper">
                <el-option v-for="item in projectList" :value="item.id" :label="item.projectName"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="检查日期" prop="recordDate" class="form-item">
              <el-date-picker
                class="filter-item"
                popper-class="popperStyle"
                v-model="editForm.recordDate"
                type="date"
                clearable
                value-format="yyyy-MM-dd"
                placeholder="选择开始时间">
              </el-date-picker>
            </el-form-item>
            <el-form-item label="是否需要整改" prop="isRectify" class="form-item">
              <el-select placeholder="请选择" style="width:100%;" clearable v-model="editForm.isRectify"
                         class="form-select" :popper-append-to-body="false" popper-class="select-popper">
                <el-option :value="0" label="不需要"></el-option>
                <el-option :value="1" label="需要"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="文件类型" prop="fileType" class="form-item">
              <el-select placeholder="请选择" style="width:100%;" clearable v-model="editForm.userType"
                         class="form-select" :popper-append-to-body="false" popper-class="select-popper">
                <el-option :value="0" label="图片"></el-option>
                <el-option :value="1" label="PDF文件"></el-option>
                <el-option :value="2" label="word文档"></el-option>
                <el-option :value="3" label="excel文件"></el-option>
                <el-option :value="4" label="PPT文件"></el-option>
                <el-option :value="9" label="其它文件"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="文件" prop="fileName" class="form-item">
              <el-button class="btn-add" type="warning" size="mini"
                         icon="el-icon-upload" @click="upLoad">点击上传
              </el-button>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="mini" @click.native="dialogFormVisible=false">取消</el-button>
        <el-button type="primary" size="mini" @click="createData">{{dialogStatus == 'create'?'添加':'编辑'}}</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import { doPost, doGet, doFile, orglistChild } from '@/api/index'
  import Pagination from '@/components/Pagination/index'

  export default {
    name: "record",
    components: {Pagination},
    data() {
      return {
        loading: false,
        list: [],
        total: 0,
        listQuery: {
          pageNum: 1,
          pageSize: 20,
          projectType: 0
        },
        dialogStatus: '',
        textMap:{
          update: '编辑',
          create: '新增'
        },
        dialogFormVisible: false,
        editForm: {
          projectType: 0,
          recordDate: '',
          isRectify: '',
          fileType: '',
          projectID: ''
        },
        editFormRules: {
        },
        projectList: []
      }
    },
    created() {
      // this.getList()
      this.getProjectList()
    },
    methods: {
      getProjectList(){
        doPost('project/page').then(res => {
          this.projectList = res.data.list
        }).catch(error => {
          this.projectList = []
        })
      },
      getList() {
        this.loading = true
        doPost('supervise/page',this.listQuery).then(res => {
          this.loading = false
          this.list = res.data.list
          this.total = res.data.total
        }).catch(error => {
          this.list = []
          this.total = 0
          this.loading = false
        })
      },
      handleAdd() {
        this.dialogFormVisible = true
        this.dialogStatus = 'create'
        this.editForm = {
          projectType: 0,
          recordDate: '',
          isRectify: '',
          fileType: '',
          projectID: ''
        }
        this.$nextTick(() => {
          this.$refs['editForm'].clearValidate()
        })
      },
      createData() {
        this.$refs['editForm'].validate(valid => {
          if (valid) {
            doPost(`supervise/${this.dialogStatus == 'create' ? 'add' : 'update'}`,this.editForm).then(res => {
              if (res.code == 200){
                this.dialogFormVisible = false
                this.getList()
                this.$message.success({
                  message: this.dialogStatus == 'create'?'提交成功':'保存成功',
                  type: 'success'
                })
              } else {
                this.$message.error(res.message)
              }
            }).catch(error => {
              this.$message.error(error.message)
            })
          }
        })
      },
      //上传文件
      upLoad(){}
    }
  }
</script>

<style scoped>

</style>
